package com.ruoyi.file.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.file.domain.File;
import com.ruoyi.file.mapper.FileMapper;
import com.ruoyi.file.service.LocalSysFileServiceImpl;
import com.ruoyi.file.service.MinioSysFileServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.system.api.domain.SysFile;

/**
 * 文件请求处理
 * 
 * @author ruoyi
 */
@RestController
public class SysFileController
{
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    FileMapper fileMapper;



    @Autowired
    private LocalSysFileServiceImpl localSysFileService;

    /**
     * 用于公告的附件上传、图片上传等
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file)
    {
        try
        {

            // 上传并返回访问地址
            String url = localSysFileService.uploadFile(file);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            return R.ok(sysFile);
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 用于资料下载的上传
     * @param file
     * @return
     */
    @PostMapping("uploadfile")
    public R<SysFile> uploadfile(MultipartFile file)
    {
        try
        {
            R<SysFile> upload = upload(file);
            SysFile sysFile = upload.getData();
            File file1 = new File();
            String url = sysFile.getUrl();
            file1.setFilename(FileUtils.getName(url));
            file1.setFilepath(sysFile.getUrl());
            fileMapper.insert(file1);
            return R.ok(sysFile);
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }
    @GetMapping("list")
    public R<Page<File>> getFileList(Long current,Long size, String fileName){
        Page<File> page = new Page<>(current, size);
        LambdaQueryWrapper<File> queryWrapper = new LambdaQueryWrapper<>();
        if (fileName != null && !"".equals(fileName)) {
            queryWrapper.like(File::getFilename, fileName);
        }
        queryWrapper.orderByDesc(File::getCreateTime);
        Page<File> filePage = fileMapper.selectPage(page, queryWrapper);
        return R.ok(filePage);
    }

    @DeleteMapping()
    public R<Page<File>> DeleteFile(Long FileId) throws Exception {
        File file = fileMapper.selectById(FileId);
        int i = fileMapper.deleteById(FileId);//暂时不删除本地文件
        boolean b = localSysFileService.deleteFile(file.getFilepath());
        if (i>0){
            return R.ok();
        }
        return R.fail();
    }

    @DeleteMapping("/byFilepath")
    public R<Page<File>> DeletebyFilepath(String Filepath) throws Exception {
        boolean b = localSysFileService.deleteFile(Filepath);
        if (b){
            return R.ok();
        }
        return R.fail();
    }
}